
********
* CPAP *
********

* DME Claims
{
use ".\statadata\dme_total_matched", clear
gen insample = 1 if hcpcs_cd=="A4604" & mod1=="NU"
replace insample = 2 if hcpcs_cd=="A7027" & mod1=="NU"
replace insample = 2 if hcpcs_cd=="A7028" & mod1=="NU"
replace insample = 2 if hcpcs_cd=="A7029" & mod1=="NU"
replace insample = 1 if hcpcs_cd=="A7030" & mod1=="NU"
replace insample = 1 if inlist(hcpcs_cd,"A7031","A7032","A7033","A7034","A7035","A7036","A7037","A7038","A7039") & mod1=="NU"
replace insample = 1 if inlist(hcpcs_cd,"A7044","A7045","A7046") & mod1=="NU"
replace insample = 1 if inlist(hcpcs_cd,"E0561","E0562")
replace insample = 3 if inlist(hcpcs_cd,"E0485","E0486")
drop if insample==.
gen yq = qofd(clm_thru_dt)
format yq %tq
collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt q = line_srvc_cnt, by(bene_id yq hcpcs_cd mod1)
compress
save ".\temp\sample_CPAP_related.dta", replace
}

* Number of Claim in 2010 as weights
{
preserve
gen year = year(dofq(yq))
keep if year==2010
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(3) nogen
collapse (sum) payment_2010=total_amt claim_2010=n_claim, by(geo_id hcpcs_cd mod1)
save ".\temp\claim_weight_CPAP_related", replace
restore
}


* MBSF: Enrollment info
{
keep bene_id
duplicates drop
merge 1:m bene_id using ".\temp\mbsf", keep(3) nogen
keep bene_id year geo_id cba_round enroll* dual* 
reshape long enroll_Q dual_Q, i(bene_id year geo_id cba_round) j(qtr)
gen yq = yq(year,qtr)
format yq %tq

keep if enroll_Q==3 
keep bene_id yq geo_id cba_round 

merge 1:m bene_id yq using ".\temp\sample_CPAP_related.dta", keep(3) nogen
save ".\temp\sample_CPAP_related.dta", replace
}

*** Regression Sample Construction ***
{
use ".\temp\sample_CPAP_related.dta", clear

gen n_bene = 1
bysort bene_id hcpcs_cd mod1: egen temp = min(yq)
gen n_initiator = 1 if temp==yq
drop temp

collapse (sum) n_bene* n_initiator* q* total_amt* n_claim*,	by(hcpcs_cd mod1 yq geo_id cba_round)
drop if yq==.
drop if geo_id==""
gen product = hcpcs_cd+"-"+mod1
egen product_id = group(product)
unique product_id
save ".\temp\sample_CPAP_related_reg.dta", replace

preserve
keep product_id hcpcs_cd mod1
duplicates drop
tempfile temp
save `temp'
restore
keep geo_id cba_round
duplicates drop
expand 28
bysort geo_id: gen yq = yq(2008,4)+_n
format yq %tq
expand 24
bysort geo_id yq: gen product_id = _n
merge 1:1 geo_id yq product_id using ".\temp\sample_CPAP_related_reg.dta", nogen
drop hcpcs_cd mod1
merge m:1 product_id using `temp', nogen
gen year = year(dofq(yq))
merge m:1 geo_id yq using ".\temp\population", keep(1 3) nogen
merge m:1 geo_id hcpcs_cd mod1 using ".\temp\claim_weight_CPAP_related", keep(1 3) nogen
replace payment_2010 = 0 if payment_2010==.
replace claim_2010 = 0 if claim_2010==.

replace q = 0 if q==.
replace n_bene = 0 if n_bene==.
replace n_initiator = 0 if n_initiator==.
replace total_amt = 0 if total_amt==.

gen qX = q/pop_FFS*1000
gen NbeneX = n_bene/pop_FFS*1000
gen NinitiatorX = n_initiator/pop_FFS*1000
gen paymentX = total_amt/pop_FFS*1000

gen lg_qX = log(qX)
gen lg_NbeneX = log(NbeneX)	
gen lg_NinitiatorX = log(NinitiatorX)	
gen lg_paymentX = log(paymentX)

gen price = total_amt/q
gen lg_price = log(price)

gen dd = 0
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 & inlist(hcpcs_cd,"A7027","A7028","A7029")==0
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1 & inlist(hcpcs_cd,"A7027","A7028","A7029")==1
save ".\temp\sample_CPAP_related_reg.dta", replace
}


*** Regression: DID Main effect (Lead) ***
use ".\temp\sample_CPAP_related_reg.dta", clear
egen geo_idnum = group(geo_id)

* A7027 - Actual CBP
{
local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=pop_FFS_2010] if hcpcs_cd=="A7027", nocons absorb(geo_idnum yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
}

* A7027 - Round 1 (spillover)
{
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 & hcpcs_cd=="A7027"

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=pop_FFS_2010] if hcpcs_cd=="A7027" & cba_round!=2 & yq<yq(2014,1), nocons absorb(geo_idnum yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA15_CPAP_mask") sheetreplace	
restore
}


* E0486 - Actual CBP (E0485 very few) LCD approval in 2011, so only R2
{
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1 

local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=pop_FFS_2010] if hcpcs_cd=="E0486" & cba_round!=1 & yq>=yq(2011,1), nocons absorb(geo_idnum yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
gen any_q = (qX!=0)
foreach ver in qX {
	cap n qui reghdfe `ver' dd [aweight=pop_FFS_2010] if hcpcs_cd=="E0486" & cba_round!=1 & yq>=yq(2011,1), nocons absorb(geo_idnum yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA14_CPAP_oraldevice") sheetreplace	
restore
}

